package com.feng.study.controller;

import com.feng.study.aspect.MyAnnotationTestAspect;
import com.feng.study.dao.UserDao;
import com.feng.study.demo.mq.RabbitMqSender;
import com.feng.study.demo.spring.TransactionalDemo;
import com.feng.study.entity.UserEntity;
import com.feng.study.enum1.DepositStatusEnum;
import com.feng.study.util.EnumUtils;
import com.feng.study.value.Student;
import com.feng.study.value.User;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@RestController
@Api(value = "测试controller",tags = {"测试相关接口"})
public class TestController {

    @Autowired
    private TransactionalDemo transactionalDemo;

    @Autowired
    private RabbitMqSender rabbitMqSender;

    @Resource
    private UserDao userDao;

//    @Autowired
//    private Processor processor;

    @Value("${xxl.job.admin.addresses:testHelloworld}")
    private String str;

    public static void main(String[] args) {

        try {
            Constructor<UserEntity> declaredConstructor = UserEntity.class.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);
            UserEntity userEntity = declaredConstructor.newInstance();
            System.out.println();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "测试代理对象")
    @GetMapping("/test/transactional")
    public void testTransactional(){
        userDao.equals(userDao);
        System.out.println("调用的代理对象。类型："+transactionalDemo.getClass());
        transactionalDemo.testInvalid();
    }

    @ApiOperation(value = "获取用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true)}
    )
    @GetMapping("/user/test")
//    @MyAnnotationTestAspect(methodName = "testAspect")
    public User test(@RequestParam("id") String id) {
        Assert.notNull(id, "入参id不能为空");
        Student student = new Student();
        student.setAge(11);
        student.setName("zhangsan");
        Integer aa = 17;
        if (aa == 1) {
            return null;
        }
        System.out.println(student);
        User user = BeanMapper.map(student, User.class);
        user.setDepositStatusCode("PENDING");
        user.setPrice(BigDecimal.valueOf(9.9356));
        user.setAnEnu(DepositStatusEnum.REFUNDED);
        return user;
    }

    @GetMapping("/jsonEnumDemo")
    public User jsonEnumDemo() {
        // 1 测试枚举工具类
        // 2 测试枚举注解json增强序列化
        String code = null;
        DepositStatusEnum pending = EnumUtils.getByCode(code, DepositStatusEnum.class);
        System.out.println(pending.getName());

        User user = new User();
        user.setDepositStatusCode(DepositStatusEnum.REFUNDED.getCode());
        user.setAnEnu(DepositStatusEnum.REFUNDED);
        return user;
    }

    @GetMapping("/user/test2")
    public void test02(@RequestParam("name") String name,@RequestParam("gender") String gender){
        System.out.println(name);
        System.out.println(gender);
    }

    @GetMapping("/productUpdate/updateEsFull")
    public String updateEsFull(@RequestParam(value = "models", required = false) List<String> models) {
        if (CollectionUtils.isNotEmpty(models)) {
            System.out.println("局部更新");
            System.out.println(models);
        } else {
            System.out.println("异步全量更");
        }
        return "全量更新开始";
    }

    @GetMapping("/testException")
    public String testException() {
        System.out.println(str);
        int a=5/0;
        System.out.println("hello");
        return "hello";
    }

    @GetMapping("/testException2")
    public String testException2() {
        int[] arr=new int[0];
        arr[11]=10;
        System.out.println(arr);
        return "hello";
    }

    @GetMapping("/testRabbitMq")
    public void testRabbitMq(){
//        try{
//            processor.testStudyOutput().send(MessageBuilder.withPayload("orderId:hello,world!").build());
//        }catch (Exception e){
//            log.error("发送消息异常。e:{}",e);
//        }
//        rabbitMqSender.send();

        System.out.println();
    }
}
